﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
#if false
    public class SpriteReferenceHolder : MonoBehaviour
    {
        private SpriteReference sr;
        public void Set(SpriteReference sr)
        {
            this.sr = sr;
        }
    }

#else
    [DisallowMultipleComponent]
    public class SpriteReferenceHolder : MonoBehaviour
    {
        private Sprite sprite;
        private bool isRelease;

        public void SetSprite(Sprite sprite)
        {
            if (this.sprite == sprite) return;
            if (this.sprite != null)
            {
                this.Release();
            }

            this.sprite = sprite;
            Retain();
        }

        private void Retain()
        {
            if (this.sprite != null && !this.isRelease)
            {
                this.isRelease = SpritePool.Instance.Retain(this.sprite);
            }
        }

        private void Release()
        {
            if (this.isRelease && this.sprite != null)
            {
                this.isRelease = false;
                SpritePool.Instance.Free(this.sprite, false);
                this.sprite = null;
            }
        }


        private void OnDestroy()
        {
            Release();
        }


    }
    #endif
}
